<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="预定日期" prop="orderDates">
        <el-date-picker
          v-model="queryParams.orderDates"
          value-format="YYYY-MM-DD"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表  -->
  <ContentWrap>
      <div v-for="(item , index) in list" :key="index">
        <div class="title">
          {{ formatDate(item.orderDate, 'YYYY-MM-DD') }}
          {{ formatDate(item.orderDate, 'dddd') }}
        </div>
        <div class="card">
          <div class="card-item" v-for="(food , index) in item.foodList" :key="index">
            <div class="img-content">
              <el-image :src="food.picture" alt="" class="img"/>
            </div>
            <div class="info-content">
              <div class="name" :title="food.name">{{ food.name }}</div>
              <dict-tag :type="DICT_TYPE.ORDERING_TYPE" :value="food.type"/>
              <div class="last-line">
                <div class="price">{{ food.price }}元</div>
                <Icon class="icon" icon="ep:star" :size='30' color="#eebe77" v-show="!food.hasOrdered" 
                title="预订" @click="handleOrder(food, item.orderDate)"/>
                <Icon class="icon" icon="ep:star-filled" :size='35' color="#eebe77" v-show="food.hasOrdered" 
                title="取消预订" @click="handleCancelOrder(food)"/>
              </div>

            </div>
          </div>
        </div>
      </div>
  </ContentWrap>

</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, formatDate, addTime } from '@/utils/formatTime'
import { FoodApi, FoodVO } from '@/api/ordering/food'
import { RecordApi, RecordVO } from '@/api/ordering/record'
import { constants } from 'buffer'


/** 餐品-订餐记录 列表 */
defineOptions({ name: 'MyOrdering' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<any[]>([]) // 列表的数据
const activeNames = ref([0,1]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  foodId: undefined,
  name: undefined,
  type: undefined,
  price: undefined,
  status: undefined,
  // 默认查询未来一周内的订餐情况
  orderDates: [formatDate(new Date()), formatDate(addTime(new Date(), 6*24*60*60*1000))],
  userId: undefined,
  userName: undefined,
  deptId: undefined,
  deptName: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await FoodApi.getMyOrderingList(queryParams)
    list.value = data
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}


/**
 * 处理订单操作
 * 
 * @param {FoodVO} food - 要订购的食物信息
 * @param {Date} orderDate - 订单日期
 */
const handleOrder = async (food: FoodVO, orderDate: Date) => {
  try {
    const record = await FoodApi.convertToRecord(food)
    record.value.orderDate = orderDate
    record.value.id = null
    await RecordApi.createRecord(record.value)
    message.success('预定成功')
    await getList()
  } catch {}
}

const handleCancelOrder = async (food: FoodVO) => {
  try {
    const record = await FoodApi.convertToRecord(food)
    record.value.status = 3
    await RecordApi.updateRecord(record.value)
    message.success('取消成功')
    await getList()
  } catch {}
}



/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
  .title{
    background: #f5f7f9;
    font-size: large;
    padding: 15px 15px 15px 0;
  }
  .card{
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;

    .card-item{
      display: flex;
      flex-direction: row;
      align-items: center;
      height: 150px;
      background: #f5f7f9;
      border-radius: 5px;
      margin: 5px;
      .img-content{
        display: flex;
        flex-direction: row;   /*设置主轴方向是水平方向*/
        align-items: center;  /*设置侧轴上，子元素的排列方式为居中对齐*/
        width: 150px;
        height: 150px;
        margin-right: 15px;
        .img{
          width: 130px;
          height: 130px;
          border-radius: 15px;
          line-height: 150px;
          margin: 0 auto;
        }

      }
      .info-content{
        width: auto;
        >*{
          padding: 8px 0;
        }

        .name{
          font-size: 24px;
          width: 100px; /* 设定容器宽度 */
          overflow: hidden;//隐藏超出的内容;
          text-overflow: ellipsis; /* 显示省略号 */
          white-space: nowrap; /* 不换行 */
        }
        .name:hover {
          /* 当鼠标悬停时，可以添加额外样式，如改变背景色 */
          background-color: rgba(0, 0, 0, 0.1); /* 添加淡入背景色 */
        }
        .price{
          color: red;
          font-weight: bold;
        }
        .icon{
          color: red;
          font-size: 20px !important;
        }
        .icon:hover{
          cursor: pointer; /* 鼠标悬停时显示为小手 */
        }
        .last-line{
          display: flex;
          flex-direction: row;
          justify-content: space-between;
          align-items: center;
          margin-right: 10px;
          
        }


      }

    }
  }
</style>
